/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.restaurant;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.restaurant.RestaurantDao;
import com.boxin.ims.modules.wechat.entity.restaurant.Restaurant;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 酒店Service
 * @author Jakemanse
 * @version 2013-11-14
 */
@Component
@Transactional(readOnly = true)
public class RestaurantService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(RestaurantService.class);
	
	@Autowired
	private RestaurantDao restaurantDao;
	
	public Restaurant get(Long id) {
		return restaurantDao.findOne(id);
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-22  下午10:06:23
	 * @function <p> 所有的 餐厅 </p>
	 * @param userId
	 * @return
	 */
	public List<Restaurant> findByUserId( Long userId) {
		DetachedCriteria dc = restaurantDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.addOrder(Order.desc("id"));
		return restaurantDao.find(dc);
	}
	
	
	public Page<Restaurant> find(Page<Restaurant> page, Restaurant restaurant) {
		DetachedCriteria dc = restaurantDao.createDetachedCriteria();
		if (restaurant.getUser()!=null && restaurant.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", restaurant.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(restaurant.getName())){
			dc.add(Restrictions.like("name", "%"+restaurant.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return restaurantDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Restaurant restaurant) {
		restaurantDao.save(restaurant);
	}
	
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		restaurantDao.deleteById(id);
	}
	
}
